package com.gitee.feizns.explore.data_structure.tree.bs.mid;

/**
 * @author feizns
 * @since 2020/3/18
 */
public class LongestZigZag {

    public static void main(String[] args) {

    }

    public static int longestZigZag(TreeNode root) {
        if ( root != null ) {
            dfs(root.left, true, 1);
            dfs(root.right, false, 1);
        }
        return max;
    }

    static int max = Integer.MIN_VALUE;

    private static void dfs(TreeNode node, boolean left, int l) {
        if ( node != null ) {
            l += 1;
            max = Math.max(max, l);
            if ( left ) {
                dfs(node.left, true, 0);
                dfs(node.right, false, l);
            } else {
                dfs(node.left, true, l);
                dfs(node.right, false, 0);
            }
        }
    }

}
